/*
 * color.c
 *
 *  Created on: Oct 1, 2011
 *      Author: gustavo
 */

#include <math.h>

#include "color.h"

#define _rgb( R, G, B) ((R << 16) | (G << 8) | (B << 0))

/*

 * div = 1;
 * nc = 2;
 * 
 * cor = 3 => 0	255	255;
 * 
 * B = 255 * (3%2) / 1;
 * B = 255 * (1) / 1; 
 * B = 255 * 1;
 * B = 255;
 
 * G = 255 * ((3/2)%2) / 1;
 * G = 255 * ((1)%2) / 1;
 * G = 255 * (1) / 1;
 * G = 255 * 1;
 * G = 255;
 
 * R = 255 * ((3/2/2)%2) / 1;
 
0	0	0 
0	0	255
0	255	0
0	255	255
255	0	0
255 0	255
255 255	0
255 255	255

 */

Color imp_color_rgb_palette(int cor, int div) {
	Color color;

	int size;
	int R, G, B;
	int nc;
	
	if( div == 0 ) {
		return imp_color(255,255,255);
	}
	
	nc = div+1;

	size = pow(3,nc);
	cor = cor % size;
	
	B = 255*( (cor%nc) )/div;
	
	G = 255*( (cor/nc)%nc )/div;
	
	R = 255*( (cor/nc/nc)%nc )/div;
	
	color.px = (R << 16) | (G << 8) | (B << 0);
	
	if( color.px == 0 && cor != 0 && cor != 64) {
		return color;
	}

	return color;
}
/* Color ==================================================================== */

Color imp_color_gray(unsigned char gray) {
	Color color;
	color.gray = gray;
	return color;
}

Color imp_color(unsigned char R, unsigned char G, unsigned char B) {
	Color color;
	color.px = (B << 16) | (G << 8) | (R << 0);
	return color;
}
